/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.tech15.core.action;

import static javacommon.util.extjs.Struts2JsonHelper.outJson;
import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseStruts2Action;
import javacommon.util.CtgData;
import javacommon.util.Notes;
import javacommon.util.ResInfo;
import javacommon.util.org.TreeData;
import cn.org.rapid_framework.web.util.HttpUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.core.model.Dept;
import com.tech15.core.service.DeptManager;
import com.tech15.core.service.ModuleManager;


/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class DeptAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String CHECKPERLIST_JSP= "/sys/dept_permission_list.jsp";
	protected static final String CREATE_JSP = "/sys/dept_add.jsp";
	protected static final String COPY_JSP = "/sys/dept_copy.jsp";
	protected static final String MOVE_JSP = "/sys/dept_move.jsp";
	protected static final String EDIT_JSP = "/sys/dept_edit.jsp";
	protected static final String SHOW_JSP = "/pages/Dept/show.jsp";
	protected static final String MANAGER_JSP = "/sys/dept_list.jsp";
	//redirect paths,startWith: !
	private DeptManager deptManager;
	private ModuleManager moduleManager;
	
	private Dept dept;
	Long id = null;
	private String[] items;
	private String key;

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			dept = new Dept();
		} else {
			dept = (Dept)deptManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setDeptManager(DeptManager manager) {
		this.deptManager = manager;
	}	
	public void setModuleManager(ModuleManager manager) {
		this.moduleManager = manager;
	}
	
	public Object getModel() {
		return dept;
	}
	
	public void setId(Long val) {
		this.id = val;
	}
	public void setDeptid(Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 获取模块列表 设置部门权限 */
	public String checkPer() {
		List<CtgData> list = moduleManager.findAuthModuleTree(id,"2",Notes.APPID);
		getRequest().setAttribute("list", list);
		return CHECKPERLIST_JSP;
	}
	
	public String manager(){
		return MANAGER_JSP;
	}
	
	public void tree(){
		List<TreeData> item = deptManager.findDeptTree();
		TreeData root = new TreeData();
		root.setId("0");
		root.setItem(item);
		outJson(root);
	}
	
	public void quickSelectDept() {
		Map map = new HashMap();
		List<Dept> list = deptManager.quickSelectDept();
		map.put("totalRecords", -1);
		map.put("root", list);
		
		outJson(map);
	}
	
	public void selectDept() {
		Map map = new HashMap();
		List<Dept> list = deptManager.selectDept();
		Dept dept = new Dept();
		dept.setName("请选择");
		list.add(0, dept);
		map.put("totalRecords", -1);
		map.put("root", list);
		outJson(map);
	}
	
	public void selectMyDept() {
		Map map = new HashMap();
		List<Dept> list = deptManager.selectDeptByDeptid(keyid,"");
		Dept dept = new Dept();
		dept.setName("请选择");
		list.add(0, dept);
		map.put("totalRecords", -1);
		map.put("root", list);
		outJson(map);
	}
	
	public void selectDeptByDeptid() throws UnsupportedEncodingException {
		Map map = new HashMap();
		String deptname = "";
		if(!isNullOrEmptyString(key)){
			deptname = new String(key.getBytes("ISO-8859-1"),"UTF-8");
		}
		List<Dept> list = deptManager.selectDeptByDeptid(keyid,deptname);
		map.put("totalRecords", 0);
		map.put("root", list);
		
		outJson(map);
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		if(dept.getPid() != null){
			Dept parent_dept = (Dept)deptManager.getById(dept.getPid());
			getRequest().setAttribute("parent_dept", parent_dept);
		}
		return CREATE_JSP;
	}
	
	/** 进入copy页面*/
	public String copy() {
		Dept p_dept = deptManager.getById(this.dept.getPid());
		if(p_dept != null){
			parentDeptName = p_dept.getDeptname();
		}
		return COPY_JSP;
	}
	
	/** 保存新增对象 */
	public void save() {
		Map<String, Object> resInfo = null;
		try {
			deptManager.save(dept);
			resInfo = ResInfo.getResInfo("", false, false, 2, OPERATE_SUCCESS, dept.getDeptid()+"");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, OPERATE_FAILED,"");
		}	
		outXmlJson(resInfo);
	}
	
	/**进入更新页面*/
	public String edit() {
		if(dept.getPid() != null){
			Dept parent_dept = (Dept)deptManager.getById(dept.getPid());
			getRequest().setAttribute("parent_dept", parent_dept);
		}
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public void update() {
		Map<String, Object> resInfo = null;
		try {
			deptManager.update(this.dept);
			resInfo = ResInfo.getResInfo("", false, false, 2, OPERATE_SUCCESS, "");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
	}
	
	public void updateMove() {
		Map<String, Object> resInfo = null;
		try {
			deptManager.update(this.dept);
			resInfo = ResInfo.getResInfo("", false, false, 2, OPERATE_SUCCESS, "");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
	}
	
	public String move() {
		Dept p_dept = deptManager.getById(this.dept.getPid());
		parentDeptName = p_dept.getDeptname();
		return MOVE_JSP;
	}
	
	public void delete() {
		Map<String, Object> resInfo = null;
		try {
			deptManager.removeById(id);
			resInfo = ResInfo.getResInfo("",false, false, 2,OPERATE_SUCCESS,"");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, "此部门拥有子部门!请先删除其下的子部门!","");
		}		
		outXmlJson(resInfo);
	}
	
	/**删除对象*/
	public void batchDelete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			Long id = new Long((String)params.get("deptid"));
			deptManager.removeById(id);
		}
	}
	
	private String parentDeptName;
	private Long keyid;

	public Long getKeyid() {
		return keyid;
	}

	public void setKeyid(Long keyid) {
		this.keyid = keyid;
	}

	public String getParentDeptName() {
		return parentDeptName;
	}

	public void setParentDeptName(String parentDeptName) {
		this.parentDeptName = parentDeptName;
	}

}
